<?php
namespace Services;

use Helpers\Tool;
use Helpers\Main;
use Helpers\HttpClient;
use Helpers\HttpResponse;
use Helpers\User;
use Helpers\XhException;
use Modules\School;

class Auth
{

    /**
     * @var \Helpers\HttpClient
     */
    public static $httpClient;
    public static $url;
    public static $enum = [
        'enabled' => [
            true  => '正常',
            false => '禁用'
        ]
    ];

    /**
     * 初始化配置
     *
     * @throws XhException
     *
     * create by ck 20180831
     */
    private static function init()
    {
        if (!self::$httpClient) {
            $config     = Main::getConfigByName('auth');
            self::$url  = Tool::getValue($config, 'url');
            if (!self::$url) {
                throw new XhException('服务url获取失败');
            }
            self::$httpClient = new HttpClient(self::$url);
        }
    }

    /**
     * 获取重复登陆列表
     *
     * @param int $page
     * @param int $size
     * @param array $search
     * @return array
     * @throws XhException
     *
     * create by ck 20180831
     */
    public static function getRepeatLogin($page = 1, $size = 50, $search = [])
    {
        self::init();
        $response = self::$httpClient->get(
            '/api/v1/pri/allowRepeatLogin',
            array_merge(['page' => $page - 1, 'size' => $size], $search)
        );
        if (!$response->isSuccess()) {
            throw new XhException('列表获取失败', $response);
        }
        $body = $response->toArray();
        $data = [
            'total' => $body['totalElements'],
            'rows'  => $body['content']
        ];
        Tool::runTimestamp($data['rows'], ['createDate', 'deadline'], false);
        Tool::runEnum($data['rows'], self::$enum);
        School::runName($data['rows'], ['schoolId' => 'schoolName']);
        Main::runUserName($data['rows'], ['userId' => 'userName']);
        return $data;
    }

    /**
     * 创建重复登陆
     *
     * @param $logonNames
     * @param $deadline
     * @return bool
     * @throws XhException
     *
     * create by ck 20180831
     */
    public static function createRepeatLogin($logonNames, $deadline)
    {
        self::init();
        $response = self::$httpClient->postJson('/api/v1/pri/allowRepeatLogin/', [
            'createUserId' => User::getId(),
            'deadLineTime' => intval($deadline),
            'logonNames'   => implode(',', $logonNames)
        ]);
        if (!$response->isSuccess()) {
            throw new XhException('创建失败', $response);
        }
        return true;
    }

    /**
     * 删除重复登陆
     *
     * @param $userId
     * @return bool
     * @throws XhException
     *
     * create by ck 20180831
     */
    public static function delRepeatLogin($userId)
    {
        self::init();
        $response = self::$httpClient->delete(sprintf('/api/v1/pri/allowRepeatLogin/%d', $userId));
        if (!$response->isSuccess()) {
            throw new XhException('删除失败', $response);
        }
        return true;
    }

    /**
     * 启用重复登陆
     *
     * @param $userId
     * @return bool
     * @throws XhException
     *
     * create by ck 20180901
     */
    public static function enableRepeatLogin($userId)
    {
        self::init();
        $response = self::$httpClient->patch(sprintf('/api/v1/pri/allowRepeatLogin/%d/enable', $userId));
        if (!$response->isSuccess()) {
            throw new XhException('启用失败', $response);
        }
        return true;
    }

    /**
     * 禁用重复登陆
     *
     * @param $userId
     * @return bool
     * @throws XhException
     *
     * create by ck 20180901
     */
    public static function disableRepeatLogin($userId)
    {
        self::init();
        $response = self::$httpClient->patch(sprintf('/api/v1/pri/allowRepeatLogin/%d/disEnable', $userId));
        if (!$response->isSuccess()) {
            throw new XhException('禁用失败', $response);
        }
        return true;
    }

    /**
     * 刷新用户缓存
     *
     * @param $userIds
     * @return bool
     * @throws XhException
     *
     * create by ck 20180903
     * modify by ck 20180928
     */
    public static function refresh($userIds)
    {
        self::init();
//        $response = self::$httpClient->get('/api/v1/pri/cache/remove/', ['userIds' => implode(',', $userIds)]);
        $userIds = array_map(function ($userId) {
            return intval($userId);
        }, $userIds);
        $response = self::$httpClient->postJson('/api/v1/pri/cache/remove/', ['userIdList' => $userIds]);
        if (!$response->isSuccess()) {
            throw new XhException('刷新失败', $response);
        }
        return true;
    }
}
